package com.fingard.dsp.salaryset;

import java.io.File;
import java.util.HashMap;

import com.fingard.FGBiz;
import com.fingard.dsp.bank.directConfig.NodeSalaryItem;
import com.fingard.dsp.bank.directConfig.SalaryConfig;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;

/**
 * Created by F on 2019/2/9.
 */
public class SalaryDecrypt {
    public static HashMap<String,SalaryConfig> hashMapSalaryConfig = null;

    public static String rsaPrivateKey;
    static {
        try {
            hashMapSalaryConfig = new HashMap<String,SalaryConfig>();
            String fileName = getConfigPath();
            if (new File(fileName).exists()) {
                String strConfig = FileHelper.readFileText(fileName, "utf-8");
                XmlTextReader xmlTextReader = new XmlTextReader(strConfig);
                rsaPrivateKey =xmlTextReader.getFirstText("/config/RSAPrivateKey");
                while (xmlTextReader.read("/config/item")) {
                    String corporation =xmlTextReader.readedXElement.getSingleText("corporation");
                    if (corporation == null || corporation.length() == 0) {
                        corporation = "5000001";
                    }
                    String[] corporationArr = corporation.split(",");
                    for(String corporationItem : corporationArr) {
                        SalaryConfig salaryConfig = new SalaryConfig();
                        salaryConfig.bankCode = xmlTextReader.readedXElement.getSingleText("bankCode");
                        salaryConfig.group = xmlTextReader.readedXElement.getSingleText("group");
                        if (salaryConfig.group == null || salaryConfig.group.length() == 0) {
                            salaryConfig.group = "Default";
                        }
                        salaryConfig.corporation = corporationItem;
                        salaryConfig.serverIp = xmlTextReader.readedXElement.getSingleText("serverIp");
                        salaryConfig.port = xmlTextReader.readedXElement.getSingleText("prot");
                        salaryConfig.org = xmlTextReader.readedXElement.getSingleText("org");
                        salaryConfig.keyID = xmlTextReader.readedXElement.getSingleText("keyID");
                        salaryConfig.nodeSalaryItemMap = new HashMap<String, NodeSalaryItem>();
                        for (XElement node : xmlTextReader.readedXElement.getChildList("nodeSalaryItem")) {
                            NodeSalaryItem item = new NodeSalaryItem();
                            item.fromXElement(node);
                            salaryConfig.nodeSalaryItemMap.put(item.tarEle, item);
                        }
                        String key = salaryConfig.bankCode + "-" + salaryConfig.corporation + "-" + salaryConfig.group;
                        hashMapSalaryConfig.put(key, salaryConfig);
                    }
                }
            } else {
                System.out.println(("发薪解密配置文件:" + fileName + " 不存在"));
            }
        }catch (Exception ex){
            ex.printStackTrace();
            System.out.println("初始化异常！");
        }
    }


    private static String getConfigPath() {
        return StringHelper.trimEnd(FGBiz.dspCenter.rootDirPath,File.separator) + File.separator + "config" + File.separator + "SalaryDecrypt.xml";
    }

    public static SalaryConfig getSalaryConfig(String bankCode) throws Exception {
        if (hashMapSalaryConfig == null) {
            hashMapSalaryConfig = new HashMap<String,SalaryConfig>();
            String fileName = getConfigPath();
            if (new File(fileName).exists()) {
                String strConfig = FileHelper.readFileText(fileName, "utf-8");
                XmlTextReader xmlTextReader = new XmlTextReader(strConfig);
                while (xmlTextReader.read("/config/item")) {

                    String corporation =xmlTextReader.readedXElement.getSingleText("corporation");
                    if (corporation == null || corporation.length() == 0) {
                        corporation = "5000001";
                    }
                    String[] corporationArr = corporation.split(",");
                    for(String corporationItem : corporationArr) {
                        SalaryConfig salaryConfig = new SalaryConfig();
                        salaryConfig.bankCode = xmlTextReader.readedXElement.getSingleText("bankCode");
                        salaryConfig.group = xmlTextReader.readedXElement.getSingleText("group");
                        if (salaryConfig.group == null || salaryConfig.group.length() == 0) {
                            salaryConfig.group = "Default";
                        }
                        salaryConfig.corporation = corporationItem;
                        salaryConfig.serverIp = xmlTextReader.readedXElement.getSingleText("serverIp");
                        salaryConfig.port = xmlTextReader.readedXElement.getSingleText("prot");
                        salaryConfig.org = xmlTextReader.readedXElement.getSingleText("org");
                        salaryConfig.keyID = xmlTextReader.readedXElement.getSingleText("keyID");
                        salaryConfig.nodeSalaryItemMap = new HashMap<String, NodeSalaryItem>();
                        for (XElement node : xmlTextReader.readedXElement.getChildList("nodeSalaryItem")) {
                            NodeSalaryItem item = new NodeSalaryItem();
                            item.fromXElement(node);
                            salaryConfig.nodeSalaryItemMap.put(item.tarEle, item);
                        }
                        String key = salaryConfig.bankCode + "-" + salaryConfig.corporation + "-" + salaryConfig.group;
                        hashMapSalaryConfig.put(key, salaryConfig);
                    }
                }
            } else {
                throw new Exception("发薪解密配置文件:" + fileName + " 不存在");
            }
        }
        return hashMapSalaryConfig.get(bankCode);

    }

    public static String getKey()throws Exception{
        String fileName = getConfigPath();
        String strConfig = FileHelper.readFileText(fileName, "utf-8");
        XmlTextReader xmlTextReader = new XmlTextReader(strConfig);
        return xmlTextReader.getFirstText("/config/decryptKey");
    }
}
